package de.maedn.main.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONValue;

public class JsonServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("application/json");
		resp.setCharacterEncoding("utf-8");
		resp.setHeader("Cache-Control", "no-cache, must-revalidate");
		resp.setHeader("Access-Control-Allow-Origin", "*");

		String method = req.getParameter("method");
		Map<String, Object> data = (Map<String, Object>) JSONValue.parse(req.getParameter("data"));

		Map<String, Object> result = null;
		if (method.equals("register")) {
			result = register(data);
		}
		JSONValue.writeJSONString(result, resp.getWriter());
	}
	
	private Map<String, Object> register(Map<String, Object> data) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("status", 0);
		result.put("userid", "id"+data.get("username"));
		return result;
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		doGet(req, resp);
	}
}